package com.winning.pmph.ctrl;


import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.winning.pmph.entity.IncreaseCategory;
import com.winning.pmph.entity.IncreaseQrCode;
import com.winning.pmph.entity.IncreaseResource;
import com.winning.pmph.service.IncreaseCategoryService;
import com.winning.pmph.service.IncreaseQrCodeService;
import com.winning.pmph.service.IncreaseResourceService;
import com.winning.pmph.service.IncreaseUploadService;
import com.winning.pmph.utils.ApplicationContextUtil;
import com.winning.pmph.utils.PMPHAppUtil;
import com.winning.pmph.utils.UploadFileUtil;
import com.winning.pmph.vo.CategorySelectVo;
import com.winning.pmph.vo.IncreaseResourceVo;
import com.winning.pmph.vo.ResourceTreeVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lbm
 * @since 2021-12-30
 */
@Api(value = "图书增值服务5")
@RestController
@RequestMapping("/pmph/action/increaseResource")
public class IncreaseResourceCtrl {
	@Autowired
    private IncreaseResourceService increaseResourceService;
	@Autowired
	private IncreaseCategoryService increaseCategoryService;
	@Autowired
	private IncreaseUploadService increaseUploadService;

	@ApiOperation(value = "selectPageList", notes = "获取增值资源列表接口")
	@GetMapping("selectPageList")
	public Page<IncreaseResourceVo> selectPageList(String name, String bookId,String categoryId,Integer currentPage, Integer pageSize) {
		if (currentPage == null || currentPage == 0) {
			currentPage = 1;
		}
		if (pageSize == null || pageSize == 0) {
			pageSize = 10;
		}
		Page<IncreaseResource> page = increaseResourceService.lambdaQuery()
				.eq(StringUtils.isNotBlank(name), IncreaseResource::getName, name)
				.eq(StringUtils.isNotBlank(bookId), IncreaseResource::getBookId, bookId)
				.eq(StringUtils.isNotBlank(categoryId), IncreaseResource::getCateId, categoryId)
				.orderByAsc(IncreaseResource::getSortNum)
				.orderByDesc(IncreaseResource::getCreateTime)
				.page(new Page<>(currentPage, pageSize));
		
		List<IncreaseResourceVo> resultList = page.getRecords().stream().map((entity)->{
			IncreaseResourceVo vo = new IncreaseResourceVo();
			BeanUtils.copyProperties(entity, vo);
			IncreaseQrCode qrCode = increaseResourceService.getQrCode(entity.getId());
			if(qrCode!=null) {
				String qrCodeName = qrCode.getName()==null?"":qrCode.getName();
				vo.setQrCodeName(qrCodeName);
			}
            return vo;
        }).collect(Collectors.toList());
		Page<IncreaseResourceVo> resultPage = new Page<>();
		resultPage.setRecords(resultList);
		resultPage.setTotal(page.getTotal());
		return resultPage;
	}
	
	@ApiOperation(value = "selectGroupResourcePageList", notes = "获取资源组下资源列表")
	@GetMapping("selectGroupResourcePageList")
	public Page<IncreaseResource> selectGroupResourcePageList(String name, String resourceId,String categoryId,Integer currentPage, Integer pageSize) {
		if (currentPage == null || currentPage == 0) {
			currentPage = 1;
		}
		if (pageSize == null || pageSize == 0) {
			pageSize = 10;
		}
		
		Page<IncreaseResource> page = null;
		if(resourceId.equals(categoryId)) {
			page = increaseResourceService.lambdaQuery()
					.eq(StringUtils.isNotBlank(name), IncreaseResource::getName, name)
					.eq(StringUtils.isNotBlank(resourceId), IncreaseResource::getResourceId, resourceId)
					.eq(IncreaseResource::getIsSub, 1)
					.orderByDesc(IncreaseResource::getCreateTime)
					.page(new Page<>(currentPage, pageSize));
		}else {
			page = increaseResourceService.lambdaQuery()
					.eq(StringUtils.isNotBlank(name), IncreaseResource::getName, name)
					.eq(StringUtils.isNotBlank(resourceId), IncreaseResource::getResourceId, resourceId)
					.eq(StringUtils.isNotBlank(categoryId), IncreaseResource::getCateId, categoryId)
					.eq(IncreaseResource::getIsSub, 1)
					.orderByDesc(IncreaseResource::getCreateTime)
					.page(new Page<>(currentPage, pageSize));
		}
		
		return page;
	}
	
	@ApiOperation(value = "selectResourceGroupTree", notes = "获取资源组分类")
	@GetMapping("selectResourceGroupTree")
	public List<ResourceTreeVo> selectResourceGroupTree(String resourceId) {
		List<ResourceTreeVo> list = increaseResourceService.getResourceGroupTree(resourceId);
		return list;
	}
	
	@ApiOperation(value = "selectResourceSelectTree", notes = "获取增值服务资源组选择树")
	@GetMapping("selectResourceSelectTree")
	public List<CategorySelectVo> selectResourceSelectTree(String name, String resourceId) {
		if (StringUtils.isBlank(resourceId)) {
			throw new RuntimeException("资源组id为空");
		}
		List<CategorySelectVo> list = increaseResourceService.getResourceSelectTree(resourceId);
		return list;
	}
	
	@ApiOperation(value = "uploadFile", notes = "导入资源")
	@RequestMapping("uploadFile")
    public Map<String, Object> uploadFile(@RequestParam("file") MultipartFile file,@RequestParam("bookId") String bookId,@RequestParam("categoryId") String categoryId) throws Exception {
        Map<String, Object> result = new HashMap<>();
        increaseResourceService.importResource(file, bookId,categoryId);
        return result;
    }
	
	@ApiOperation(value = "uploadResource", notes = "上传资源文件")
	@RequestMapping("uploadResource")
    public Map<String, Object> uploadFile(@RequestParam("file") MultipartFile file) throws Exception {
        Map<String, Object> result = new HashMap<>();
        String path = "increaseresource/";
        String savePath = ApplicationContextUtil.BASE_DOWN_FORDER + path;
        String name = UploadFileUtil.uploadFile(file, savePath, true);
        result.put("path", "/"+path+name);
        result.put("originalName", file.getOriginalFilename());
        return result;
    }
	
	@ApiOperation(value = "batchUploadResource", notes = "批量更新资源")
	@RequestMapping("batchUploadResource")
    public List<Map<String, String>> batchUploadResource(@RequestBody IncreaseResourceVo increaseResourceVo) throws Exception {
		return increaseUploadService.batchUploadResource(increaseResourceVo);
    }
	
	@ApiOperation(value = "importGroupResourceInfo", notes = "导入资源组子资源")
	@RequestMapping("importGroupResourceInfo")
    public Map<String, Object> importGroupResourceInfo(@RequestParam("file") MultipartFile file,@RequestParam("resourceId") String resourceId,@RequestParam("categoryId") String categoryId) throws Exception {
        Map<String, Object> result = new HashMap<>();
        increaseResourceService.importGroupResource(file, resourceId,categoryId);
        return result;
    }
	
	@ApiOperation(value = "batchImportGroupResource", notes = "导入资源组子资源的实体资源")
	@RequestMapping("batchImportGroupResource")
    public List<Map<String, String>> batchImportGroupResource(@RequestBody IncreaseResourceVo increaseResourceVo) throws Exception {
        return increaseResourceService.importGroupResourceFile(increaseResourceVo);
    }
	
	@ApiOperation(value = "editResource", notes = "编辑资源")
	@RequestMapping("editResource")
    public void editResource(@RequestBody IncreaseResourceVo increaseResourceVo) throws Exception {
		increaseUploadService.editResource(increaseResourceVo);
    }
	
	@ApiOperation(value = "addResource", notes = "新增资源")
	@RequestMapping("addResource")
    public void addResource(@RequestBody IncreaseResourceVo increaseResourceVo) throws Exception {
		increaseUploadService.addResource(increaseResourceVo);
    }
	
	/**
	 * 添加资源组
	 * @param increaseResourceVo
	 * @return
	 * @throws Exception
	 */
	@ApiOperation(value = "create", notes = "添加资源组")
	@RequestMapping("create")
    public void create(@RequestBody IncreaseResourceVo increaseResourceVo) throws Exception {
        increaseResourceService.createResourceGroup(increaseResourceVo);
    }
	
	/**
	 * 添加组下的资源
	 * @param increaseResourceVo
	 * @return
	 * @throws Exception
	 */
	@ApiOperation(value = "addGroupResource", notes = "添加资源组下的资源")
	@RequestMapping("addGroupResource")
    public void addGroupResource(@RequestBody IncreaseResourceVo increaseResourceVo) throws Exception {
		increaseResourceService.addGroupResource(increaseResourceVo);
    }
	
	/**
	 * 添加资源组分类
	 * @param increaseResourceVo
	 * @return
	 * @throws Exception
	 */
	@ApiOperation(value = "addGroupCate", notes = "添加资源组分类")
	@RequestMapping("addGroupCate")
    public void addGroupCate(@RequestBody IncreaseResourceVo increaseResourceVo) throws Exception {
        increaseResourceService.addGroupCate(increaseResourceVo);
    }
	
	@ApiOperation(value = "selectById", notes = "获取增值资源")
	@GetMapping("selectById")
	public IncreaseResourceVo selectById(String id) {
		IncreaseResource resource = increaseResourceService.getById(id);
		
		IncreaseResourceVo vo = new IncreaseResourceVo();
		BeanUtils.copyProperties(resource, vo);
		String cateId = resource.getCateId();
		IncreaseCategory increaseCategory = increaseCategoryService.getById(cateId);
		if(increaseCategory!=null) {
			vo.setCateName(increaseCategory.getName());
		}
		IncreaseQrCode increaseQrCode= increaseResourceService.getQrCode(id);
		if(increaseQrCode!=null) {
			vo.setQrCodeName(increaseQrCode.getName());
		}
		return vo;
	}
	
	@ApiOperation(value = "delete", notes = "删除资源")
	@GetMapping("delete")
	public void delete(String id) {
		increaseResourceService.removeById(id);
        String userName = PMPHAppUtil.getCurrentUserEnName();
        increaseResourceService.sendDeleteResourceData(id, userName);
	}
	
	@ApiOperation(value = "updateResource", notes = "更新资源信息")
	@RequestMapping("updateResource")
    public void updateResource(@RequestBody IncreaseResourceVo increaseResourceVo) throws Exception {
        IncreaseResource resource = new IncreaseResource();
        resource.setId(increaseResourceVo.getId());
        resource.setCateId(increaseResourceVo.getCateId());
        increaseResourceService.updateById(resource);
    }
	
	@ApiOperation(value = "updateGroupCateName", notes = "更新资源组分类名称")
	@RequestMapping("updateGroupCateName")
    public void updateGroupCateName(@RequestBody IncreaseResourceVo increaseResourceVo) throws Exception {
        IncreaseResource resource = new IncreaseResource();
        resource.setId(increaseResourceVo.getId());
        resource.setName(increaseResourceVo.getName());
        increaseResourceService.updateById(resource);
    }
	
	@ApiOperation(value = "upOrDownResource", notes = "上下移动资源")
	@RequestMapping("upOrDownResource")
    public void upOrDownResource(@RequestBody IncreaseResourceVo increaseResourceVo) throws Exception {
        increaseResourceService.upOrDownResource(increaseResourceVo);
    }
	
	/**
	 * 上下移动资源组分类
	 * @param increaseResourceVo
	 * @return
	 * @throws Exception
	 */
	@ApiOperation(value = "upOrDownGroupCate", notes = "上下移动资源组分类")
	@RequestMapping("upOrDownGroupCate")
    public void upOrDownGroupCate(@RequestBody IncreaseResourceVo increaseResourceVo) throws Exception {
        increaseResourceService.upOrDownResourceCate(increaseResourceVo);
    }
}

